package com.csx.framework.springboot.sample.mvc.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 自定义的请求拦截器
 * @author cuisongxu
 * @date 2024/12/22 周日 16:04
 */
public class CustomHandlerInterceptor implements HandlerInterceptor {

    private final Logger logger = LoggerFactory.getLogger(CustomHandlerInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("CustomHandlerInterceptor 请求处理前 request{}, response{}",request,response);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.info("CustomHandlerInterceptor 请求处理后 request{}, response{}",request,response);
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("CustomHandlerInterceptor 请求结束 request{}, response{}",request,response);
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
